<?php

class ControllerAdmin_Structure extends ControllerAdmin_Base
{
	public function __construct($section) {		
		$this->section      = "structure";
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."structure";
	}
	
	public function init()
	{
		$this->structure = new structure($this->table_module);		
		$this->structure->mode = $this->section;
		$this->structure->active_group = ($_GET["id"] ? $_GET["id"] : 1);		
		$this->structure->sorting = ' ORDER BY `Por`';
		$this->structure->field = array(						
			'edit'   => true,
			'por'    => true,
			'show'   => true,
			'delete' => true,			 
			'field_cfg' => array(
				array(
					'name'      => 'Название раздела',
					'fieldname' => 'name',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				),
				array(
					'name'      => 'Системное имя',
					'fieldname' => 'sys_name',
					'width'     => '50%',
					'link'      => true,			
					'attr'      => false
				)
			),
			"aoData" => array(
				"bSortable" => array(
					"false",
					"false",
					"false",
					"false",
					"null",
					"null"
				)
			)
		);
	}
	
	public function addToTable()
	{
		if(!empty($_POST['type']))
		{				
			extract($_POST);			
			$date_update = date("Y-m-d H:i:s");					
			if ($_POST['type'] == 'add') {
				$date_create = date("Y-m-d H:i:s");
				eval(functions::insert_sql($this->table_module, 'sql'));
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
			elseif($_POST['type'] == 'editt') {
				eval(functions::update_sql($this->table_module, 'sql', 'id', $_POST['id']));				
				$r =& Registry::get("MDB")->query($sql);		
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
			}
		}
	}
	
    public function out()
	{
		self::init();		
		self::addToTable();
		
		if(empty($_GET['type'])) {
			$this->action = '<div class="addbuttons"><a href="?m=structure&parent='.$this->structure->active_group.'&type=add" class="ui_wo_buttons ui-state-default ui-corner-all"><span class="ui-icon ui-icon-circle-plus"></span>Добавить раздел</a></div>';			
			return $this->structure->FirstPage();
		}		
		else
		{
			if($_GET['type'] == 'edit')
			{							   
				$r =& Registry::get("MDB")->query("SELECT * FROM `".$this->table_module."` WHERE `ID`='".$_GET['id']."' LIMIT 1;");
				if (PEAR::isError($r)) {
					die($r->getMessage().' ::: '.$r->getUserInfo());
				}
				if($r->numRows()) {
					$Field = $r->fetchRow(MDB2_FETCHMODE_ASSOC);
				}
			}			
			elseif($_GET['type'] == 'add' || empty($_GET['type'])) {
				$Field["por"] = functions::incrPor($this->table_module, $_GET["parent"]);			
			}
			
			if($this->structure->pInfo['auto_increment'] && empty($_GET['id'])) {
				$id_action = $this->structure->pInfo['auto_increment'];
			}
			elseif(!empty($_GET['id'])) {
				$id_action = $_GET['id'];
			}
			
			/* Список модулей */
			$r =& Registry::get("MDB")->query("SELECT `ID`,`Name`,`Controller` FROM `".PRFX."modules` ORDER BY `Por`;");
			if (PEAR::isError($r)) {
				die($r->getMessage().' ::: '.$r->getUserInfo());
			}
			if($r->numRows()) {
				while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
					$modules_list[$p["id"]] = $p["name"]." :: ".$p["controller"];
				}
			}
			
			/* Список тепмлейт */
			$r =& Registry::get("MDB")->query("SELECT `ID`,`Name`,`Tpl` FROM `".PRFX."templates` ORDER BY `Por`;");
			if (PEAR::isError($r)) {
				die($r->getMessage().' ::: '.$r->getUserInfo());
			}
			if($r->numRows()) {
				while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
					$tpl_list[$p["id"]] = $p["name"]." :: ".$p["tpl"];
				}
			}
			Registry::get("smarty")->assign(array(
				'SECTION'   => $this->section,				
				'ID_ACTION' => $id_action,
				'Field'     => $Field,
				'MODULES_LIST' => $modules_list,
				'TPL_LIST' => $tpl_list,
				'GROUPS'   => groups::get($this->table_module)
			));
			return Registry::get("smarty")->fetch(TEMPLATE_ADMIN.$this->common_vars["controller_template"], Registry::get("cache_id"));
		}		
    }
}

$controller = new ControllerAdmin_Structure($section);
$ob_admin_out = $controller->out();
$ob_admin_action = $controller->action;

?>